package info.tsukaworld.world;

import info.tsukaworld.world.*;
import info.tsukaworld.grid.*;
import info.tsukaworld.actor.*;
import info.tsukaworld.gui.PlayArea;

public class BasicWorld extends ActorWorld
{
    private Player player;
    private Grid<Actor> gr;
    private Ladder up;
    private Ladder down;
    private PlayArea area;

    public BasicWorld(Grid<Actor> g, Player p, PlayArea a)
    {
        super(g);
        gr = g;
        player = p;
        area = a;
        up = new Ladder(this, "UpLadder.gif");
        down = new Ladder(this, "DownLadder.gif");
        newFloor(0);
    }

    public void step()
    {
        if( up.isActive() )
        {

            //floor--;
            up.toggle();
            /*
            if(floor == 0)
            {
                clearFloor();
                startFloor();
            }
            else if(floor == -1)
            {
                clearFloor();
                bossFloor();
            }
            else*/
                newFloor(0);
            area.drawTerrain();
        }
        else if ( down.isActive() )
        {
            //floor++;
            down.toggle();
            /*
            if(floor == 0)
            {
                clearFloor();
                bossTurn = 0;
                startFloor();
            }
            else if(floor == 20)
            {
                clearFloor();
                bossFloor();
            }
            else*/
                newFloor(1);
            area.drawTerrain();
        }
        else
        {
            super.step();
        }
    }

    public void newFloor(int direction)
    {
        //Clear old floor
        clearFloor();

        //Horizontal walls
        for(int i = 1; i < this.getGrid().getNumCols() - 1; i++)
        {
            if( i != this.getGrid().getNumCols() / 2 && i != this.getGrid().getNumCols() / 6 && 
            i != 5 * this.getGrid().getNumCols() / 6)
            {
                add(new Location(this.getGrid().getNumRows()/3, i), new Terrain("Wall.gif", false, "Wall"));
                add(new Location(2 * this.getGrid().getNumRows()/3, i), new Terrain("Wall.gif", false, "Wall"));
            }
        }

        //Vertical Walls
        for(int i = 1; i < this.getGrid().getNumRows() - 1; i++)
        {
            if( i != this.getGrid().getNumRows() / 2 && i != this.getGrid().getNumRows() / 6 && 
            i != 5 * this.getGrid().getNumRows() / 6)
            {
                add(new Location(i , this.getGrid().getNumCols()/3), new Terrain("Wall.gif", false, "Wall"));
                add(new Location(i , 2 * this.getGrid().getNumCols()/3), new Terrain("Wall.gif", false, "Wall"));
            }
        }

        //Horizontal Border
        for(int i = 0; i < this.getGrid().getNumCols(); i++)
        {
            add(new Location(0, i), new Terrain("Wall.gif", false, "Wall"));
            add(new Location(this.getGrid().getNumRows() - 1, i), new Terrain("Wall.gif", false, "Wall"));
        }

        //Vertical Border
        for(int i = 0; i < this.getGrid().getNumRows(); i++)
        {
            add(new Location(i, 0), new Terrain("Wall.gif", false, "Wall"));
            add(new Location(i, this.getGrid().getNumCols() - 1), new Terrain("Wall.gif", false, "Wall"));
        }

        //fillCreeps();
        //fillItems();

        //Place up ladder
        Location ladderLoc = new Location((int)(Math.random() * this.getGrid().getNumRows()), 
                (int)(Math.random() *this.getGrid().getNumCols()));
        while( this.getGrid().get( ladderLoc ) != null || getGrid().getEmptyAdjacentLocations( ladderLoc ).size() < 1)
        {
            ladderLoc = new Location((int)(Math.random() * this.getGrid().getNumRows()), 
                (int)(Math.random() *this.getGrid().getNumCols()));
        }
        add(ladderLoc, up);

        //Add Player adjacent to up ladder
        if( direction == 1 )
        {
            add( getGrid().getEmptyAdjacentLocations( ladderLoc ).get(0), player );
            player.setDirection( player.getLocation().getDirectionToward( ladderLoc ) + 180 );
        }

        //Place down ladder
        ladderLoc = ladderLoc = new Location((int)(Math.random() * this.getGrid().getNumRows()), 
            (int)(Math.random() *this.getGrid().getNumCols()));
        while( this.getGrid().get( ladderLoc ) != null || getGrid().getEmptyAdjacentLocations( ladderLoc ).size() < 1)
        {
            ladderLoc = new Location((int)(Math.random() * this.getGrid().getNumRows()), 
                (int)(Math.random() *this.getGrid().getNumCols()));
        }

        add(ladderLoc, down);

        //Add player adjacent to down ladder
        if( direction == 0 )
        {
            add( getGrid().getEmptyAdjacentLocations( ladderLoc ).get(0), player );
            player.setDirection( player.getLocation().getDirectionToward( ladderLoc ) + 180 );
        }

        //add( getRandomEmptyLocation(), new Slime());
        //add( getRandomEmptyLocation(), new Bomb());
    }

    public void clearFloor()
    {
        for(int r = 0; r < getGrid().getNumRows(); r++)
        {
            for(int c = 0; c < getGrid().getNumCols(); c++)
            {
                if(getGrid().get(new Location(r, c)) != null)
                    getGrid().get(new Location(r, c)).removeSelfFromGrid();
            }
        }
    }

}
